﻿///////////////////////////////////////////////////////////////////////////////////Date Format////////////////////////////////////////////////////////////
var dateFormat = function () {
    var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
		timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
		timezoneClip = /[^-+\dA-Z]/g,
		pad = function (val, len) {
		    val = String(val);
		    len = len || 2;
		    while (val.length < len) val = "0" + val;
		    return val;
		};

    // Regexes and supporting functions are cached through closure
    return function (date, mask, utc) {
        var dF = dateFormat;

        // You can't provide utc if you skip other args (use the "UTC:" mask prefix)
        if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
            mask = date;
            date = undefined;
        }

        // Passing date through Date applies Date.parse, if necessary
        date = date ? new Date(date) : new Date;
        if (isNaN(date)) throw SyntaxError("invalid date");

        mask = String(dF.masks[mask] || mask || dF.masks["default"]);

        // Allow setting the utc argument via the mask
        if (mask.slice(0, 4) == "UTC:") {
            mask = mask.slice(4);
            utc = true;
        }

        var _ = utc ? "getUTC" : "get",
			d = date[_ + "Date"](),
			D = date[_ + "Day"](),
			m = date[_ + "Month"](),
			y = date[_ + "FullYear"](),
			H = date[_ + "Hours"](),
			M = date[_ + "Minutes"](),
			s = date[_ + "Seconds"](),
			L = date[_ + "Milliseconds"](),
			o = utc ? 0 : date.getTimezoneOffset(),
			flags = {
			    d: d,
			    dd: pad(d),
			    ddd: dF.i18n.dayNames[D],
			    dddd: dF.i18n.dayNames[D + 7],
			    m: m + 1,
			    mm: pad(m + 1),
			    mmm: dF.i18n.monthNames[m],
			    mmmm: dF.i18n.monthNames[m + 12],
			    yy: String(y).slice(2),
			    yyyy: y,
			    h: H % 12 || 12,
			    hh: pad(H % 12 || 12),
			    H: H,
			    HH: pad(H),
			    M: M,
			    MM: pad(M),
			    s: s,
			    ss: pad(s),
			    l: pad(L, 3),
			    L: pad(L > 99 ? Math.round(L / 10) : L),
			    t: H < 12 ? "a" : "p",
			    tt: H < 12 ? "am" : "pm",
			    T: H < 12 ? "A" : "P",
			    TT: H < 12 ? "AM" : "PM",
			    Z: utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
			    o: (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
			    S: ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
			};

        return mask.replace(token, function ($0) {
            return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
        });
    };
} ();

// Some common format strings
dateFormat.masks = {
    "default": "ddd mmm dd yyyy HH:MM:ss",
    shortDate: "m/d/yy",
    mediumDate: "mmm d, yyyy",
    longDate: "mmmm d, yyyy",
    fullDate: "dddd, mmmm d, yyyy",
    shortTime: "h:MM TT",
    mediumTime: "h:MM:ss TT",
    longTime: "h:MM:ss TT Z",
    isoDate: "yyyy-mm-dd",
    isoTime: "HH:MM:ss",
    isoDateTime: "yyyy-mm-dd'T'HH:MM:ss",
    isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};

// Internationalization strings
dateFormat.i18n = {
    dayNames: [
		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
		"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
	],
    monthNames: [
		"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
		"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
	]
};

// For convenience...
Date.prototype.format = function (mask, utc) {
    return dateFormat(this, mask, utc);
};
// ===================================================================
// feedAlert Documentation:
// msgText is a string of text that is displayed inside message alert. (i.e. 'Saved Successfully')
// msgStyle is a string that indicates the css class name used for styling the message alert. (i.e. 'msgGreen' or 'msgRed'.)
// msgType is an integer that determines the user interaction of the message alert

// Different Message Types
// 1 = Message alert is displayed and then fades away after 1.5 seconds.
// 2 = Message alert is displayed with user-activated close button.
// 3 = Message alert is displayed without user-activated close button and does not close until closefeedAlert() is called within script.

//Use this function to create feedAlert windows
function feedAlert(msgText, msgStyle, msgType) {
    //In case  feedAlert has already fired
    document.getElementById('feedAlert').style.display = 'none';

    //Insert dynamic message
    document.getElementById('msgText').innerHTML = msgText;

    //Style dynamic Message
    document.getElementById('feedAlert').className = msgStyle;

    // jQuery FX
    $('#feedAlert').animate({ opacity: 1.0 }, 100).fadeIn();

    if (msgType == 2) {
        document.getElementById('btnClose').style.visibility = "visible";
        $('#btnClose').animate({ opacity: 1.0 }, 100).fadeIn();
    }
    if (msgType == 1) {
        $('#feedAlert').animate({ opacity: 1.0 }, 1500).fadeOut();
    }
}

//Use this function to force-close feedAlert windows
function closefeedAlert() {
    $('#feedAlert').animate({ opacity: 1.0 }, 100).fadeOut();
    $('#btnClose').animate({ opacity: 1.0 }, 100).fadeOut();
};

// ===================================================================





function AlertInfo(message) {
    //    $.jnotifica(message, {
    //        position: 'top',
    //        width: 500,
    //        msgCss: {
    //            textAlign: 'center'
    //        },
    //        background: 'green', 
    //        opacity: 1,
    //        margin: 30,
    //        clickClose: false,
    //        timeout: 2000,
    //        classes: 'rounded_corners'
    //    });

}

function AlertWarning(message) {
    //    $.jnotifica(message, {
    //        position: 'top',
    //        width: 300,
    //        msgCss: {
    //            textAlign: 'center'
    //        },
    //        background: 'orange',
    //        margin: 30,
    //        opacity: 1,
    //        classes: 'rounded_corners'
    //    });

}

function AlertError(message) {
    //    $.jnotifica(message, {
    //        position: 'top',
    //        width: 300,
    //        msgCss: {
    //            textAlign: 'center'
    //        },
    //        background: 'red',
    //        opacity: 1,
    //        margin: 30,
    //        classes: 'rounded_corners'
    //    });

}
function MM_swapImgRestore() { //v3.0
    var i, x, a = document.MM_sr; for (i = 0; a && i < a.length && (x = a[i]) && x.oSrc; i++) x.src = x.oSrc;
}
function MM_preloadImages() { //v3.0
    var d = document; if (d.images) {
        if (!d.MM_p) d.MM_p = new Array();
        var i, j = d.MM_p.length, a = MM_preloadImages.arguments; for (i = 0; i < a.length; i++)
            if (a[i].indexOf("#") != 0) { d.MM_p[j] = new Image; d.MM_p[j++].src = a[i]; } 
    }
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
    var i, j = 0, x, a = MM_swapImage.arguments; document.MM_sr = new Array; for (i = 0; i < (a.length - 2); i += 3)
        if ((x = MM_findObj(a[i])) != null) { document.MM_sr[j++] = x; if (!x.oSrc) x.oSrc = x.src; x.src = a[i + 2]; }
}

function HtmlEncode(html) {
    return $('<div/>').text(html).html();
}

function HtmlDecode(text) {

    return $('<div/>').html(text).text();
}

function UrlEncode(value) {

    return encodeURIComponent(value);

}

function UrlDecode(value) {

    return decodeURIComponent(value);

}

function Strip_Html(OriginalString) {
    var StrippedString = OriginalString.replace(/(<([^>]+)>)/ig, "");

    return StrippedString;
}

function truncate_word(str, length, suffix) {
    if (str.length <= length) {
        return str;
    }
    if (suffix == undefined) {
        suffix = '...';
    }

    return str.substr(0, length).replace(/\s+?(\S+)?$/g, '') + suffix;
};

function GetIframe(frameId) {
    var ifrm = document.getElementById(frameId);
    ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument;

    return ifrm;
}

function getViewportHeight() {
    if (window.innerHeight != window.undefined) return window.innerHeight;
    if (document.compatMode == 'CSS1Compat') return document.documentElement.clientHeight;
    if (document.body) return document.body.clientHeight;
    return window.undefined;
}
function getViewportWidth() {
    if (window.innerWidth != window.undefined) return window.innerWidth;
    if (document.compatMode == 'CSS1Compat') return document.documentElement.clientWidth;
    if (document.body) return document.body.clientWidth;
    return window.undefined;
}

function getBodyHeight() {

    var height,
        scrollHeight,
        offsetHeight;

    if (document.height) {

        height = document.height;

    } else if (document.body) {

        if (document.body.scrollHeight) {
            height = scrollHeight = document.body.scrollHeight;
        }

        if (document.body.offsetHeight) {
            height = offsetHeight = document.body.offsetHeight;
        }

        if (scrollHeight && offsetHeight) {
            height = Math.max(scrollHeight, offsetHeight);
        }
    }

    return height;
}

function displayPopup(width, height, dialog, mask) {

    var gPopupContainer = document.getElementById(dialog);
    var gPopupMask = document.getElementById(mask);
    if (width == null || isNaN(width)) {
        width = gPopupContainer.offsetWidth;
    }
    if (height == null) {
        height = gPopupContainer.offsetHeight;
    }
    var fullHeight = getViewportHeight();
    var fullWidth = getViewportWidth();
    var fullMaskHeight = $(document).height();
    var fullMaskWidth = $(document).width();

    // scLeft and scTop changes by Thomas Risberg

    var scLeft, scTop;

    if (self.pageYOffset) {
        scLeft = self.pageXOffset;
        scTop = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        scLeft = document.documentElement.scrollLeft;
        scTop = document.documentElement.scrollTop;
    } else if (document.body) {
        scLeft = document.body.scrollLeft;
        scTop = document.body.scrollTop;
    }

    gPopupMask.style.height = fullMaskHeight + "px";
    gPopupMask.style.width = fullMaskWidth + "px";
    gPopupMask.style.top = scTop + "px";
    gPopupMask.style.left = scLeft + "px";
    gPopupMask.style.display = "block";

    var topMargin = scTop + ((fullHeight - (height)) / 2);
    if (topMargin < 0) { topMargin = 0; }

    gPopupContainer.style.height = height + "px";
    gPopupContainer.style.width = width + "px";
    gPopupContainer.style.top = topMargin + "px";
    gPopupContainer.style.left = (scLeft + ((fullWidth - width) / 2)) + "px";
    gPopupContainer.style.display = "block";
}

function displayPopup_2(width, height, dialog) {
    var gPopupContainer = document.getElementById(dialog);
    if (width == null || isNaN(width)) {
        width = gPopupContainer.offsetWidth;
    }
    if (height == null) {
        height = gPopupContainer.offsetHeight;
    }
    var fullHeight = getViewportHeight();
    var fullWidth = getViewportWidth();

    // scLeft and scTop changes by Thomas Risberg

    var scLeft, scTop;

    if (self.pageYOffset) {
        scLeft = self.pageXOffset;
        scTop = self.pageYOffset;
    } else if (document.documentElement && document.documentElement.scrollTop) {
        scLeft = document.documentElement.scrollLeft;
        scTop = document.documentElement.scrollTop;
    } else if (document.body) {
        scLeft = document.body.scrollLeft;
        scTop = document.body.scrollTop;
    }

    var topMargin = scTop + ((fullHeight - (height)) / 2);
    if (topMargin < 0) { topMargin = 0; }

    gPopupContainer.style.height = height + "px";
    gPopupContainer.style.width = width + "px";
    gPopupContainer.style.top = topMargin + "px";
    gPopupContainer.style.left = (scLeft + ((fullWidth - width) / 2)) + "px";
    gPopupContainer.style.display = "block";
}

function GetImageUrl(imageSource, width, height, crop, quality) {
    if (quality == 0) quality = 75;

    return "/ImageHandler.axd?imageSource=" + imageSource + "&width=" + width + "&height=" + height + "&crop=" + crop + "&quality=" + quality;
}

function ShowErrorMessage(message, title) {

    jAlert(message, title);
}

function CreateBookmarkLink() {
    var url = location.href;
    var title = "My favourite";
    if (window.sidebar) { // firefox
        window.sidebar.addPanel(title, url, "");
    } else if (document.all) { //MSIE
        window.external.AddFavorite(url, title);
    } else {
        alert("Sorry, your browser doesn't support this");
    }
}

function toppage()
{ window.scrollTo(0, 0); }

function showOpenSendToFriend(url) {
    $("#dialog").dialog('destroy');
    $("#dialog").dialog({
        title: 'Gửi cho bạn bè',
        autoOpen: false,
        draggable: true,
        modal: true,
        height: 400,
        width: 500,
        buttons: {

    },
    dragStop: function (event, ui) { $(this.parentNode).css({ height: "440px" }); }
});
$("#dialog").html('<iframe id="modalIframeId" width="100%" height="100%" marginWidth="0" marginHeight="0" frameBorder="0" scrolling="no" />').dialog("open");
$("#modalIframeId").attr("src", url);
window.focus();
return false;
}

//centering popup
function centerPopup(obj, zindex) {
    //request data for centering
    var scrollPosition = $(window).scrollTop();
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();
    var popupHeight = $(obj).height();
    var popupWidth = $(obj).width();
    var a = windowHeight / 2 - popupHeight / 2;
    var b = windowWidth / 2 - popupWidth / 2;
    a += scrollPosition - 20;
    //centering
    zindex = (zindex != undefined && zindex != null) ? zindex : 99998;
    $(obj).css({
        "position": "absolute",
        /*"position": "fixed",*/
        "z-index": zindex,
        "top": a,
        "left": b
    });
    //only need force for IE6

    var a = $(document).height();
    var b = $(window).width();

    $("#backgroundPopup").css({
        "height": a,
        "width": b,
        "position": 'absolute',
        "z-index": 10000,
        'top': 0,
        'left': 0
    });

}

function centerObjectPopup(obj) {
    //request data for centering
    var windowWidth = $(window).width();
    var windowHeight = $(window).height();
    var popupHeight = $(obj).height();
    var popupWidth = $(obj).width();
    var a = windowHeight / 2 - popupHeight / 2;
    var b = windowWidth / 2;
    //centering    
    $(obj).css({
        "position": "absolute",
        /*"position": "fixed",*/
        "z-index": 99998,
        "top": a,
        "left": b
    });

    var a = $(document).height();
    var b = $(window).width();

    $("#backgroundPopup").css({
        "height": a,
        "width": b,
        "position": 'absolute',
        "z-index": 10000,
        'top': 0,
        'left': 0
    });

}

function centerDocumentPopup(obj) {
    //request data for centering
    var windowWidth = $(document).width();
    var windowHeight = $(document).height();
    var popupHeight = $(obj).height();
    var popupWidth = $(obj).width();
    var a = windowHeight / 2 - popupHeight;
    var b = windowWidth / 2 - popupWidth;
    //centering    
    $(obj).css({
        "position": "absolute",
        /*"position": "fixed",*/
        "z-index": 99998,
        "top": a,
        "left": b
    });

    var a = $(document).height();
    var b = $(window).width();

    $("#backgroundPopup").css({
        "height": a,
        "width": b,
        "position": 'absolute',
        "z-index": 10000,
        'top': 0,
        'left': 0
    });
}

//loading popup with jQuery magic!
function loadPopup(obj) {
    //loads popup only if it is disabled
    $("#backgroundPopup").css({
        "background": "#000",
        "opacity": "0.8"
    });
    $("#backgroundPopup").show();
    $(obj).fadeIn('normal');
}

//disabling popup with jQuery magic!
function disablePopup(obj) {
    //disables popup only if it is enabled   
    $("#backgroundPopup").hide();
    $(obj).hide();
}

(function ($) {
    window.JsUtils = {
            ShowLoader: function (el, msg) {
               $(el).block({ 
                  message: '<img src="/Content/images/loading.gif" />&nbsp; ' + msg,
                  css: {
                    border: '3px solid #D8D8D8',
                    padding: '15px',
        
                    '-webkit-border-radius': '10px',
                    '-moz-border-radius': '10px',
                    opacity: 0.95,
                    color: '#900'/*,
                     top:  $(el).offset().top + 'px', 
                            left: $(el).offset().left+ 'px'*/
                  },
                  overlayCSS: { backgroundColor: '#000' },
                  centerY: 0, centerX: 0
                  }); 
              } //end method
              ,
              ShowPopupLoader: function (el, msg) {
               $(el).block({ 
                  message: '<img src="/Content/images/loading.gif" />&nbsp; ' + msg,
                  css: {
                    border: '3px solid #D8D8D8',
                    padding: '15px',        
                    '-webkit-border-radius': '10px',
                    '-moz-border-radius': '10px',
                    opacity: .8,
                    color: '#900'
                  },
                  overlayCSS: { backgroundColor: '#E8E8E8' },
                  centerY: 0, centerX: 0
                  }); 
              } //end method
              , hideLoader: function(el){
                $(el).unblock();
              },cidxQuestionDialog: function (text, on_ok, cls) {
            if (typeof cls == "undefined") cls = "warning-dialog";
            var overlay = $('<div></div>');
            overlay.addClass('cidx-dialog-overlay').appendTo(document.body).css('display', 'none');
            //fixed all screen
            var a = $(document).height();
            overlay.css({
                "height": a
            });
            var dialog = document.createElement('div');
            $(dialog).html('<div class="cidx-dialog-text">' + text + '</div><div class="cidx-dialog-buttons"><div class="cidx-dialog-ok">Yes</div> <div class="cidx-dialog-cancel">Cancel</div></div>').addClass('cidx-dialog').addClass(cls);
            dialog.d_overlay = overlay;
            overlay.append(dialog);
            dialog.btn_save = $('.cidx-dialog-ok', dialog);
            dialog.btn_cancel = $('.cidx-dialog-cancel', dialog);
            dialog.on_ok = on_ok;
            dialog.btn_save.click(function () {
                dialog.on_ok();
                $(this).unbind('click');
                dialog.d_overlay.fadeOut(function () {
                    dialog.d_overlay.remove();
                });

            });
            dialog.btn_cancel.click(function () {
                dialog.d_overlay.fadeOut(function () {
                    dialog.d_overlay.remove();
                });
            });
            var scrollPosition = $(window).scrollTop();
            var windowHeight = $(window).height();
            var popupHeight = $(dialog).height();
            var marginTop = windowHeight / 2 - popupHeight;
            marginTop += scrollPosition - 100;
            $(dialog).css("margin-top", marginTop);
            dialog.d_overlay.fadeIn();
        }
          };
  })(jQuery);